from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
import unittest
class Jiuxian(unittest.TestCase):
    def setUp(self) -> None:
        print('begin case')
    def tearDown(self) -> None:
        print('end case')
    def test_zheng(self):
        driver = webdriver.Edge()
        driver.get('https://www.jiuxian.com/')
        driver.maximize_window()
        # 点击登录切换到登录界面
        driver.find_element(By.CLASS_NAME, 'headerLogin').click()
        time.sleep(4)
        driver.refresh()
        driver.forward()
        driver.set_window_size(1200,1000)
        print(driver.title)
        time.sleep(2)
        driver.back()
        driver.maximize_window()
        driver.find_element(By.CLASS_NAME, 'headerLogin').click()
        # 输入账号密码
        driver.find_element(By.ID, 'userName').send_keys('17306824601')
        driver.find_element(By.ID, 'password').send_keys('PLMqaz123')
        time.sleep(12)
        # 点击登录
        driver.find_element(By.NAME, 'bi_Usuallogin').click()
        time.sleep(3)
        driver.find_element(By.CLASS_NAME, 'bindSub').click()
        time.sleep(2)
        user = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/p/span').text
        print(user)
        self.assertTrue(user == 'jxw846672828')
        # 点击商品模块
        driver.find_element(By.CSS_SELECTOR,
                            'body > div.midHeader > div > div.headerSearch > div.hotWords > p > a:nth-child(6)').click()
        #切换窗口
        driver.switch_to.window(driver.window_handles[-1])
        # 断言店铺信息
        dianpu = driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[2]/div[2]/a').text
        print(dianpu)
        assert '国台' in dianpu
        jianjie = driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[2]/div[2]/p[2]').text
        print(jianjie)
        assert jianjie == '店铺介绍：大道至简 王道藏锋'
        time.sleep(4)
        #点击模块中要添加的商品
        driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[6]/ul/li[1]/div[2]/div[1]/a/img').click()
        driver.switch_to.window(driver.window_handles[-1])
        # ActionChains(driver).double_click(shangpin).perform()
        # ActionChains(driver).move_to_element(shangpin).perform()
        name=driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/span').text
        print(name)
        self.assertTrue(name=='53°国台国耀500ml')
        time.sleep(2)
        # 点击加入购物车
        addcart=driver.find_element(By.NAME,'detail_jiarugouwuche')
        ActionChains(driver).context_click(addcart).perform()
        ActionChains(driver).double_click(addcart).perform()
        time.sleep(3)
if __name__ == '__main__':
    #第一种执行方法
    unittest.main()
    #第二种执行方法
    unittest.TestLoader.loadTestsFromTestCase('Jiuxian')
    #第三种执行方法
    suite=unittest.TestSuite.addTest('test_zheng')
    unittest.TextTestRunner.run(suite)
